﻿using System;
using System.Windows;
using System.Windows.Controls;
using NetBay.Core.BusinessEntities.Historic;
using Telerik.Windows.Controls;

namespace Rio.Framework.Pages.Controls.Historic
{
    public class HistoricIndexColumn : GridViewBoundColumnBase
    {
        public override FrameworkElement CreateCellElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem)
        {
            HistoricInformation historicInformation = dataItem as HistoricInformation;

            StackPanel st = new StackPanel();

            foreach (var item in historicInformation.Indexes)
            {
                string value = item.IndexValue;

                TimeSpan ts;

                if (TimeSpan.TryParse(item.IndexValue, out ts))
                {
                    value = ts.ToString("0:hh:mm");
                }
                else
                {
                    DateTime dt;

                    if (DateTime.TryParse(item.IndexValue, out dt))
                    {
                        value = dt.ToString("dd/MM/yyyy");
                    }
                }
                
                TextBlock tb = new TextBlock()
                {
                    Text = String.Format("{0} = \"{1}\"", item.IndexLabel, value),
                    Margin = new Thickness(5)
                };

                st.Children.Add(tb);
            }

            return st;
        }
    }
}
